package edu.pet.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.User;

public class LoginServlet extends HttpServlet {


	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type;
		String username;
		String password;
		type = request.getParameter("type")==null?"":request.getParameter("type");
		username = request.getParameter("username");
		password = request.getParameter("password");
		PetDAO dao = new PetDAO();
		HttpSession session = request.getSession();
		if( type.equals("logout") ){
			session.removeAttribute("login");
			response.sendRedirect("../index.jsp");
			session.setAttribute("msg", "退出成功，欢迎下次使用！");
		}else{
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			if(dao.checkUser(user)){
				session.setAttribute("login", user);
				session.setAttribute("msg", "登陆成功！");
				response.sendRedirect("../user/user.jsp");
			}else{
				session.setAttribute("msg", "用户名或密码错误！");
				response.sendRedirect("../index.jsp");
			}
		} 
	}


	public void init() throws ServletException {
		
	}

}
